package xyz.chenyongpeng.orderproviderdubbo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.chenyongpeng.entity.ZsGTicketsPrice;
import xyz.chenyongpeng.entity.ZsGoods;
import xyz.chenyongpeng.orderproviderdubbo.mapper.ZsGTicketsPriceMapper;
import xyz.chenyongpeng.orderproviderdubbo.mapper.ZsGoodsMapper;
import xyz.chenyongpeng.service.IZsGoodsService;
import xyz.chenyongpeng.to.GoodsTo;
import xyz.chenyongpeng.to.TicketPriceList;
import xyz.chenyongpeng.utils.HtmlUtil;
import xyz.chenyongpeng.utils.ImgUrl;
import xyz.chenyongpeng.utils.LowerPrice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ZsGoodsService extends ServiceImpl<ZsGoodsMapper, ZsGoods> implements IZsGoodsService {
    @Autowired
    private ZsGoodsMapper zsGoodsMapper;

    @Autowired
    private ZsGTicketsPriceMapper zsGTicketsPriceMapper;

    @Override
    public List<GoodsTo> getListByLimit(Map<String,Object> parms) {
        String limit = parms.get("limit").toString();
        String[] pages = limit.split(",");

        parms.put("start", Integer.parseInt(pages[0]));
        parms.put("end", Integer.parseInt(pages[1]));
        List<GoodsTo> list = zsGoodsMapper.getListByLimit(parms);


        List<GoodsTo> mylist = new ArrayList<>();
        for (GoodsTo goods: list) {
            GoodsTo goodsTo = new GoodsTo();
            goodsTo.setGoods_img(ImgUrl.setFullUrl(goods.getGoods_img()));
            goodsTo.setPrice(LowerPrice.getLowerPrice(goods.getPrice()));
            goodsTo.setOri_price(LowerPrice.getLowerPrice(goods.getOri_price()));
            goodsTo.setDiscount_amount(LowerPrice.getLowerPrice(goods.getDiscount_amount()));
            goodsTo.setGoods_info(HtmlUtil.html2Text(goods.getGoods_info()).substring(0, 200));

            if (goods.getGt_id() == 2){
//                门票存在阶梯票价
                int good_id = goods.getGoods_id();
                ZsGTicketsPrice zsGTicketsPrice = zsGTicketsPriceMapper.selectOne((QueryWrapper)Wrappers.query().eq("goods_id", good_id));
                goodsTo.setPrice(LowerPrice.getLowerPrice(zsGTicketsPrice.getPrice().toString()));
                goodsTo.setOri_price(LowerPrice.getLowerPrice(zsGTicketsPrice.getOriPrice().toString()));
                goodsTo.setDiscount_amount(LowerPrice.getLowerPrice(zsGTicketsPrice.getDiscountAmount().toString()));
            }

            mylist.add(goodsTo);
        }
        return mylist;
    }

    @Override
    public GoodsTo getOne(Map<String, Object> parms) {
        GoodsTo goodsTo = null;
        ZsGoods zsGoods = zsGoodsMapper.selectOne(new QueryWrapper<ZsGoods>().eq("goods_id", parms.get("parms")));
        if (zsGoods != null){
            goodsTo = new GoodsTo();
            goodsTo.setGoods_id(zsGoods.getGoodsId());
            goodsTo.setGt_id(zsGoods.getGtId());
            goodsTo.setR_id(zsGoods.getRId());
            goodsTo.setGoods_title(zsGoods.getGoodsTitle());
            goodsTo.setGoods_info(zsGoods.getGoodsInfo());
            goodsTo.setGoods_img(zsGoods.getGoodsImg());
            goodsTo.setPrice(zsGoods.getPrice()+"");
            goodsTo.setOri_price(zsGoods.getOriPrice()+"");
            goodsTo.setDiscount_amount(zsGoods.getDiscountAmount()+"");
            goodsTo.setShow_label(Integer.parseInt(zsGoods.getShowLabel()));
            goodsTo.setHot_tab(zsGoods.getHotTab());
            goodsTo.setIs_appointment(zsGoods.getIsAppointment());
            goodsTo.setAssist_msg(zsGoods.getAssistMsg());
            goodsTo.setAssist_end_time(zsGoods.getAssistEndTime());
            goodsTo.setAssist_start_time(zsGoods.getAssistStartTime());
            goodsTo.setStatus(zsGoods.getStatus());
            goodsTo.setBuy_cut_time(zsGoods.getBuyCutTime());

            if (zsGoods.getGtId() == 2){
                int good_id = zsGoods.getGoodsId();

                List<TicketPriceList> ticketPriceLists = new ArrayList<>();
                List<ZsGTicketsPrice> zsGTicketsPriceList = zsGTicketsPriceMapper.selectList(new QueryWrapper<ZsGTicketsPrice>().eq("goods_id", good_id).orderByDesc("ticket_type"));
                if (zsGTicketsPriceList.size() != 0){
                    for (ZsGTicketsPrice ticketsPrice:zsGTicketsPriceList) {
                        TicketPriceList price = new TicketPriceList(ticketsPrice.getPrice(),ticketsPrice.getOriPrice(),ticketsPrice.getDiscountAmount());
                        ticketPriceLists.add(price);
                    }
                }
                goodsTo.setPrice_lists(ticketPriceLists);
            }
        }
        return goodsTo;
    }

    @Override
    public HashMap<String, Object> getGoodsAttr(Map<String, Object> parms) {
        return zsGoodsMapper.getGoodsAttr(parms);
    }

    @Override
    public int getListCount(Map<String, Object> parms) {
        return zsGoodsMapper.getListCount(parms);
    }
}
